﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class AlbumBLL
    {
        public AlbumCls CreateModel(string ActiveLanguage, string AlbumId)
        {
            return CreateModel(Environments.ConnectionString, ActiveLanguage, AlbumId);
        }

        public AlbumCls CreateModel(string ConnectionString,string ActiveLanguage,string AlbumId)
        {
            string Query = "select * from TableAlbum where AlbumId=@AlbumId or left(AlbumId,8)=@AlbumId";
            SqlConnection sqlConnection = new SqlConnection(ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@AlbumId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumId"].Value = AlbumId;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            if (dtResult.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                AlbumCls AlbumCls = new AlbumCls();
                AlbumCls.AlbumId = (string)dtResult.Rows[0]["AlbumId"];
                AlbumCls.Subject = "";
                try
                {
                    AlbumCls.Subject = (string)dtResult.Rows[0][ActiveLanguage + "Subject"];
                }
                catch { }
                AlbumCls.AlbumType = "";
                try
                {
                    AlbumCls.AlbumType = (string)dtResult.Rows[0]["AlbumType"];
                }
                catch { }
                AlbumCls.belongSite = "";
                try
                {
                    AlbumCls.belongSite = (string)dtResult.Rows[0]["belongSite"];
                }
                catch { }
                try
                {
                    AlbumCls.Owner = (string)dtResult.Rows[0]["Owner"];
                }
                catch { }
                return AlbumCls;
            }
        }


        public AlbumItemCls CreateModelItem(string AlbumItemId)
        {
            string Query = "select * from TableAlbumItem where AlbumItemId=@AlbumItemId or left(AlbumItemId,8)=@AlbumItemId";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@AlbumItemId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumItemId"].Value = AlbumItemId;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            if (dtResult.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                AlbumItemCls AlbumItemCls = new AlbumItemCls();
                AlbumItemCls.AlbumItemId = (string)dtResult.Rows[0]["AlbumItemId"];
                AlbumItemCls.ShortId = (string)dtResult.Rows[0]["ShortId"];
                AlbumItemCls.AlbumId = (string)dtResult.Rows[0]["fk_Album_Id"];
                AlbumItemCls.Subject = (string)dtResult.Rows[0]["Subject"];
                AlbumItemCls.IndexOrder = (int)dtResult.Rows[0]["IndexOrder"];
                AlbumItemCls.setDefaulView = (bool)dtResult.Rows[0]["setDefaulView"];
                
                return AlbumItemCls;
            }
        }



        public DataTable Reading(string belongSite)
        {
            string Query = "select * from TableAlbum where belongSite='" + belongSite+"'";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }

        public DataTable ReadingForSyncFromTool(string ConnectionString,string belongSite)
        {
            string Query = "select * from TableAlbum where belongSite=@belongSite";
            SqlConnection sqlConnection = new SqlConnection(ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@belongSite", SqlDbType.VarChar);
            sqlCommand.Parameters["@belongSite"].Value = belongSite;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }
        
        public DataTable ReadingAlbumItems(string AlbumId)
        {
            string Query = "select * from TableAlbumItem where fk_Album_Id=@AlbumId order by [IndexOrder]";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@AlbumId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumId"].Value = AlbumId;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }

        public string AddAlbum(SqlTransaction sqlTransaction, string belongSite, string ActiveLanguage, string Subject, string AlbumType)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string AlbumId = System.Guid.NewGuid().ToString();
            string Query = "Insert into TableAlbum(AlbumId,belongSite,Subject,[" + ActiveLanguage + "Subject],AlbumType) values (@AlbumId,@belongSite,@Subject,@Subject,@AlbumType)";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@AlbumId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumId"].Value=AlbumId;

            sqlCommand.Parameters.Add("@Subject", SqlDbType.NVarChar);
            sqlCommand.Parameters["@Subject"].Value = Subject;

            sqlCommand.Parameters.Add("@belongSite", SqlDbType.NVarChar);
            sqlCommand.Parameters["@belongSite"].Value = belongSite;

            sqlCommand.Parameters.Add("@AlbumType", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumType"].Value = AlbumType;

            try
            {
                sqlCommand.ExecuteNonQuery();

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
            return AlbumId;
        }

        public string AddAlbumItem(
            SqlTransaction sqlTransaction, 
            string AlbumId,
            string Subject,
            string AdvImageId,
            int IndexOrder)
        {
            AlbumCls AlbumCls = new AlbumBLL().CreateModel(Environments.ActiveLanguage, AlbumId);
            
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string ActiveLanguage=Environment.Environments.ActiveLanguage;
            string AlbumItemId = System.Guid.NewGuid().ToString();
            string Query = "Insert into TableAlbumItem(AlbumItemId,belongSite,ShortId,Subject,[" + ActiveLanguage + "Subject],fk_Album_Id,fk_AdvImage_Id,IndexOrder,setDefaulView) values (@AlbumItemId,@belongSite,@ShortId,@Subject,@Subject,@fk_Album_Id,@fk_AdvImage_Id,@IndexOrder,@setDefaulView)";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            
            sqlCommand.Parameters.Add("@AlbumItemId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumItemId"].Value = AlbumItemId;

            sqlCommand.Parameters.Add("@belongSite", SqlDbType.NVarChar);
            sqlCommand.Parameters["@belongSite"].Value = AlbumCls.belongSite;

            sqlCommand.Parameters.Add("@ShortId", SqlDbType.VarChar);
            sqlCommand.Parameters["@ShortId"].Value = AlbumItemId.Substring(0,8);

            sqlCommand.Parameters.Add("@Subject", SqlDbType.NVarChar);
            sqlCommand.Parameters["@Subject"].Value = Subject;

            sqlCommand.Parameters.Add("@fk_Album_Id", SqlDbType.VarChar);
            sqlCommand.Parameters["@fk_Album_Id"].Value = AlbumId;

            sqlCommand.Parameters.Add("@fk_AdvImage_Id", SqlDbType.VarChar);
            sqlCommand.Parameters["@fk_AdvImage_Id"].Value = AdvImageId;


            sqlCommand.Parameters.Add("@IndexOrder", SqlDbType.Int);
            sqlCommand.Parameters["@IndexOrder"].Value = IndexOrder;

            sqlCommand.Parameters.Add("@setDefaulView", SqlDbType.Bit);
            sqlCommand.Parameters["@setDefaulView"].Value = false;
            

            
            try
            {
                sqlCommand.ExecuteNonQuery();

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
            return AlbumItemId;
        }


        public void SaveAlbumItem(
            SqlTransaction sqlTransaction, 
            string ActiveLanguage,
            string AlbumItemId, 
            string Subject, 
            int IndexOrder)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query = " Update TableAlbumItem set [" + ActiveLanguage + "Subject]=@Subject,IndexOrder=@IndexOrder where AlbumItemId=@AlbumItemId";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@AlbumItemId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumItemId"].Value = AlbumItemId;

            sqlCommand.Parameters.Add("@Subject", SqlDbType.NVarChar);
            sqlCommand.Parameters["@Subject"].Value = Subject;

            sqlCommand.Parameters.Add("@IndexOrder", SqlDbType.Int);
            sqlCommand.Parameters["@IndexOrder"].Value = IndexOrder;

         
            try
            {
                sqlCommand.ExecuteNonQuery();

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }



        public void UpdateAlbum(SqlTransaction sqlTransaction, string ActiveLanguage, string AlbumId, string Subject, string AlbumType)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query = "Update TableAlbum set [" + ActiveLanguage + "Subject]=@Subject, AlbumType=@AlbumType where AlbumId=@AlbumId";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@AlbumId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumId"].Value = AlbumId;

            sqlCommand.Parameters.Add("@Subject", SqlDbType.NVarChar);
            sqlCommand.Parameters["@Subject"].Value = Subject;

            sqlCommand.Parameters.Add("@AlbumType", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumType"].Value = AlbumType;

            try
            {
                sqlCommand.ExecuteNonQuery();
                
                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }



        public void SetDefaultView(
           SqlTransaction sqlTransaction,
           string AlbumItemId,
            string AlbumId
          )
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query = " Update TableAlbumItem set setDefaulView=0 where fk_Album_Id=@AlbumId;" +
                " Update TableAlbumItem set setDefaulView=1 where AlbumItemId=@AlbumItemId;";

            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();

            sqlCommand.Parameters.Add("@AlbumId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumId"].Value = AlbumId;

            sqlCommand.Parameters.Add("@AlbumItemId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumItemId"].Value = AlbumItemId;

            try
            {
                sqlCommand.ExecuteNonQuery();


                DataSet ds = new DataSet("xml");
                ds.Tables.Add("dtAlbum");
                ds.Tables[0].Columns.Add("AlbumItemId");
                
                ds.Tables[0].Rows.Add(new object[]{
                    AlbumItemId
                    
                });
                string XmlData = ds.GetXml();
                //string RetValue = "";
                //if (Environments.UseFrontEndDatabase)
                //{
                //    Environment.FrontEndWebService.asmx.Service[] WServices=FrontendWebServiceController.CreateWebService();
                //    for (int iIndexService = 0; iIndexService < WServices.Length; iIndexService++)
                //    {
                //        RetValue = WServices[iIndexService].SetOnTop(Environments.SecurityCode, XmlData);

                //        if (RetValue.Trim().Length > 0)
                //        {
                //            throw new Exception(RetValue);
                //        }
                //    }
                //}


                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }

        public void DeleteAlbum(SqlTransaction sqlTransaction, string AlbumId)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            Environment.Permission.DeletePermission(sqlTransaction, AlbumId);
            string Query =
                            " Delete from TableAlbumItem where fk_Album_Id=@AlbumId"+
                            " Delete from TableAlbum where AlbumId=@AlbumId";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@AlbumId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumId"].Value = AlbumId;

            try
            {
                sqlCommand.ExecuteNonQuery();

                //if (Portal.Environment.Environments.UseFrontEndDatabase)
                //{
                //    DataSet ds = new DataSet("xml");
                //    ds.Tables.Add("Album");
                //    ds.Tables[0].Columns.Add("AlbumId");
                    
                //    ds.Tables[0].Rows.Add(new object[]{
                //        AlbumId
                //    });

                //    string XmlData = ds.GetXml();
                //    Environment.FrontEndWebService.asmx.Service[] WServices=FrontendWebServiceController.CreateWebService();
                //    for (int iIndexService = 0; iIndexService < WServices.Length; iIndexService++)
                //    {
                //        string RetValue = WServices[iIndexService].DeleteAlbum(
                //            Environments.SecurityCode, XmlData);
                //        if (RetValue.Trim().Length > 0)
                //        {
                //            throw new Exception("Webservice:" + RetValue);
                //        }
                //    }
                //}
                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }


        public void DeleteAlbumItem(SqlTransaction sqlTransaction, string AlbumItemId)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query =
                        " Delete from TableAlbumItem where AlbumItemId=@AlbumItemId";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@AlbumItemId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumItemId"].Value = AlbumItemId;

            try
            {
                sqlCommand.ExecuteNonQuery();

                //if (Portal.Environment.Environments.UseFrontEndDatabase)
                //{
                //    DataSet ds = new DataSet("xml");
                //    ds.Tables.Add("Album");
                //    ds.Tables[0].Columns.Add("AlbumItemId");

                //    ds.Tables[0].Rows.Add(new object[]{
                //        AlbumItemId
                //    });

                //    string XmlData = ds.GetXml();
                //    Environment.FrontEndWebService.asmx.Service[] WServices=FrontendWebServiceController.CreateWebService();
                //    for (int iIndexService = 0; iIndexService < WServices.Length; iIndexService++)
                //    {
                //        string RetValue = WServices[iIndexService].DeleteAlbumItem(
                //            Environments.SecurityCode, XmlData);
                //        if (RetValue.Trim().Length > 0)
                //        {
                //            throw new Exception("Webservice:" + RetValue);
                //        }
                //    }
                //}
                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }


        public void SyncWithServer(
          string ConnectionString,
          string AlbumId,
          bool UseWsForDeploy,
          string ActiveLanguage,
          string PathRoot,
          string WorkingPath,
          string PortalAlias,
          string WsUrl,
          string WsSecurityCode)
        {
            AlbumCls oAlbumCls = new AlbumBLL().CreateModel(ConnectionString,ActiveLanguage, AlbumId);
            
            DataSet ds = new DataSet();
            string Query = "";
            if (UseWsForDeploy)
            {
                Query =
                    " select * from TableAlbum where AlbumId=@AlbumId;" +
                    " select * from TableAlbumItem where fk_Album_Id=@AlbumId";
                SqlConnection sqlConnectionCheck = new SqlConnection(ConnectionString);
                sqlConnectionCheck.Open();
                SqlCommand sqlCommandCheck = sqlConnectionCheck.CreateCommand();
                sqlCommandCheck.CommandText = Query;
                sqlCommandCheck.Parameters.Clear();
                sqlCommandCheck.Parameters.Add("@AlbumId", SqlDbType.VarChar);
                sqlCommandCheck.Parameters["@AlbumId"].Value = AlbumId;

                SqlDataAdapter sqlda = new SqlDataAdapter(sqlCommandCheck);
                sqlda.Fill(ds);

                ds.Tables[0].TableName = "TableAlbum";
                ds.Tables[1].TableName = "TableAlbumItem";

                string XmlData = ds.GetXml();
                Framework.AppWs.WsReturnParamCls oWsReturnParamCls = Portal.Environment.Environments.CreateAppWs(WsUrl).SyncData(
                    WsSecurityCode,
                    AlbumId,
                    oAlbumCls.belongSite,
                    XmlData,
                    null,
                    null,
                    null,
                    Framework.AppWs.ESycDataType.Album);
                if (oWsReturnParamCls.Error) throw new Exception("WS: " + oWsReturnParamCls.ErrorMsg);
            }
        }




        public void SyncWithServer(
            string AlbumId)
        {
            string WsUrl = Environments.GetConfigFromXml("AppWs");
            string WsSecurityCode = Portal.Environment.Environments.WsSecurityCode;
            bool UseWsForDeploy = Environments.UseWsForDeploy;
            SyncWithServer(
                    Environments.ConnectionString,
                    AlbumId,
                    UseWsForDeploy,
                    Environments.ActiveLanguage,
                    Environments.PathRoot,
                    Environments.WorkingPath,
                    Environments.PortalAlias,
                    WsUrl,
                    WsSecurityCode
                );
        }


        public static string GetViewAlbum(string belongSite, string Subject, string ShortId, bool UseGallery)
        {
            Subject = Environments.ConvertSubject(Subject);

            string Url = "";
            string AppendAlias = Environments.AppendAlias;
            if (AppendAlias.Trim().Length > 0)
            {
                AppendAlias = AppendAlias + "-";
            }
            AppendAlias = AppendAlias + belongSite;
            if (UseGallery == false)
            {
                Url = Environments.FrontHttpRoot + "/" + AppendAlias + "/" + Subject + "-va-" + ShortId + "." + Environments.Extension;//?NewsId=" + ShortId;
            }
            else
            {
                Url = Environments.FrontHttpRoot + "/" + AppendAlias + "/" + Subject + "-vas-" + ShortId + "." + Environments.Extension;//?NewsId=" + ShortId;
            }
            if (Environments.MultiLanguageInSite == 1)
            {
                Url = Url + "?lang=" + Environments.ActiveLanguage;
            }
            return Url;
        }



        public static string GetViewImage(string belongSite, string Subject, string ShortId, bool UseGallery)
        {
            Subject = Environments.ConvertSubject(Subject);

            string Url = "";
            string AppendAlias = Environments.AppendAlias;
            if (AppendAlias.Trim().Length > 0)
            {
                AppendAlias = AppendAlias + "-";
            }
            AppendAlias = AppendAlias + belongSite;
            if (UseGallery == false)
            {
                Url = Environments.FrontHttpRoot + "/" + AppendAlias + "/" + Subject + "-vid-" + ShortId + "." + Environments.Extension;//?NewsId=" + ShortId;
            }
            else
            {
                Url = Environments.FrontHttpRoot + "/" + AppendAlias + "/" + Subject + "-vids-" + ShortId + "." + Environments.Extension;//?NewsId=" + ShortId;
            }
            if (Environments.MultiLanguageInSite == 1)
            {
                Url = Url + "?lang=" + Environments.ActiveLanguage;
            }
            return Url;
        }

        public void UpdateAvatar(
          SqlTransaction sqlTransaction,
          string AlbumId,
          string AvatarFileName,
          byte[] b)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query = " Update TableAlbum set AvatarFileName=@AvatarFileName,Avatar=@Avatar where AlbumId=@AlbumId";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            string ln = new System.IO.FileInfo(AvatarFileName).Name;

            string Owner = Environments.CurrentLoginUser.LoginName;
            sqlCommand.Parameters.Add("@AlbumId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AlbumId"].Value = AlbumId;

            sqlCommand.Parameters.Add("@AvatarFileName", SqlDbType.NVarChar);
            sqlCommand.Parameters["@AvatarFileName"].Value = ln;

            sqlCommand.Parameters.Add("@Avatar", SqlDbType.Image);
            sqlCommand.Parameters["@Avatar"].Value = b;


            try
            {
                sqlCommand.ExecuteNonQuery();

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }
    }
}
